//
//  CYMine_GuanZhuFriendViewController.m
//  cyydt
//
//  Created by chu on 2019/1/29.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMine_GuanZhuFriendViewController.h"
#import "CYMine_FriendTableViewCell.h"
#import "CYFriendModel.h"

@interface CYMine_GuanZhuFriendViewController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSString *_uid;
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@end

@implementation CYMine_GuanZhuFriendViewController

- (instancetype)initWithUid:(NSString *)uid{
    if (self = [super init]) {
        _uid = uid;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.view addSubview:self.tableView];
    [self getData];
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYMine_FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYMine_FriendTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYMine_FriendTableViewCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CYFriendModel *model = self.dataSourceArr[indexPath.row];
    cell.model = model;
    cell.block = ^{
        if ([model.isAttent isEqualToString:@"0"]) {
            //未关注
            [self guanzhuWithUserId:model.userId];
        }else{
            [self cancleGuanZhuWithUserId:model.userId];
        }
    };
    return cell;
}

#pragma mark - 关注取消关注
- (void)cancleGuanZhuWithUserId:(NSNumber *)userid{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",User_HOST, @"attention",userid];
    NSDictionary *param = @{@"userId":userid};
    [[XBRequest sharedInstance] deleteDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"取消关注 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [self getData];
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (void)guanzhuWithUserId:(NSNumber *)userid{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention"];
    NSDictionary *param = @{@"toUserId":userid};
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"添加关注 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [self getData];
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }else{
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                
            }
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CYFriendModel *model = self.dataSourceArr[indexPath.row];
    CYGeRenZhuYeViewController *zhuye = [[CYGeRenZhuYeViewController alloc] init];
    zhuye.userId = model.userId;
    [[AppDelegate sharedAppDelegate] pushViewController:zhuye];
}

- (void)getData{
    self.page = 1;
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention/showMyAttention"];
    NSDictionary *param = @{};
    if (!self.uid) {
        param = @{@"pageNo":@1, @"pageSize":@"10"};
    }else{
        if ([self.uid isEqualToString:[[YLUserInfo shareUserInfo].userId stringValue]]) {
            param = @{@"pageNo":@1, @"pageSize":@"10"};
        }else{
            param = @{@"uId":self.uid, @"pageNo":@1, @"pageSize":@"10"};
        }
    }
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"好友列表 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        [self.tableView removeFromSuperview];
        [self.view addSubview:self.tableView];
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSArray *list = data[@"users"];
                [self.dataSourceArr removeAllObjects];
                for (NSDictionary *dic in list) {
                    CYFriendModel *model = [CYFriendModel mj_objectWithKeyValues:dic];
                    [self.dataSourceArr addObject:model];
                }
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
            [self.tableView reloadData];

        }
    }];
}

- (void)getMoreData{
    self.page ++;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"attention/showMyAttention"];
    NSDictionary *param = @{};
    if (!self.uid) {
        param = @{@"pageNo":[NSNumber numberWithInteger:self.page], @"pageSize":@"10"};
    }else{
        if ([self.uid isEqualToString:[[YLUserInfo shareUserInfo].userId stringValue]]) {
            param = @{@"pageNo":[NSNumber numberWithInteger:self.page], @"pageSize":@"10"};
        }else{
            param = @{@"uId":self.uid, @"pageNo":[NSNumber numberWithInteger:self.page], @"pageSize":@"10"};
        }
    }
    
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"首页推荐 ---- %@",responseResult);
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.page -- ;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSArray *list = data[@"users"];
                for (NSDictionary *dic in list) {
                    CYFriendModel *model = [CYFriendModel mj_objectWithKeyValues:dic];
                    [self.dataSourceArr addObject:model];
                }
                [self.tableView reloadData];
            }else{
                self.page -- ;
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

#pragma mark - DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"wuneir"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"当前页面没有内容！";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:RGBOF(0x999999)
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    // 空白页面被点击时开启动画，reloadEmptyDataSet
    [self getData];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self getMoreData];
        }];
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"CYMine_FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYMine_FriendTableViewCell"];
    }
    return _tableView;
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}


@end
